package cn.sczhyt.modules.szhy.service.impl;

import cn.sczhyt.common.exception.RRException;
import cn.sczhyt.common.utils.Constant;
import cn.sczhyt.common.utils.R;
import cn.sczhyt.modules.sys.dao.SysUserDao;
import cn.sczhyt.modules.sys.dao.SysUserInfoDao;
import cn.sczhyt.modules.sys.dao.SysUserRoleDao;
import cn.sczhyt.modules.sys.entity.SysUserEntity;
import cn.sczhyt.modules.sys.entity.SysUserInfoEntity;
import cn.sczhyt.modules.sys.entity.SysUserRoleEntity;
import cn.sczhyt.modules.szhy.dao.MeetingMenuDao;
import cn.sczhyt.modules.szhy.dao.MeetingUserDao;
import cn.sczhyt.modules.szhy.dao.UserGroupDao;
import cn.sczhyt.modules.szhy.entity.MeetingMenuEntity;
import cn.sczhyt.modules.szhy.entity.MeetingUserEntity;
import cn.sczhyt.modules.szhy.entity.UserGroupEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.sczhyt.modules.szhy.dao.MeetingDao;
import cn.sczhyt.modules.szhy.entity.MeetingEntity;
import cn.sczhyt.modules.szhy.service.MeetingService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static cn.sczhyt.common.utils.ShiroUtils.getUserId;


@Service("meetingService")
public class MeetingServiceImpl extends ServiceImpl<MeetingDao, MeetingEntity> implements MeetingService {
    @Autowired
    private MeetingDao meetingDao;
    @Autowired
    private MeetingUserDao meetingUserDao;
    @Autowired
    private UserGroupDao userGroupDao;
    @Autowired
    private SysUserRoleDao userRoleDao;
    @Autowired
    private SysUserInfoDao userInfoDao;
    @Autowired
    private SysUserDao userDao;
    @Autowired
    private MeetingMenuDao meetingMenuDao;

    @Override
    public IPage<MeetingEntity> findList(Integer page, Integer limit, String userId, MeetingEntity meetingEntity) {
        Page<MeetingEntity> pages = new Page<>(page, limit);

        return meetingDao.findList(pages, userId, meetingEntity);
    }

    @Override
    public IPage<MeetingEntity> appFindList(String userId, Integer page, Integer limit, MeetingEntity meetingEntity) {

        SysUserRoleEntity userRoleEntity = userRoleDao.selectByUserId(userId);
        Page<MeetingEntity> pages = new Page<>(page, limit);
        //普通用户
        if (userRoleEntity.getRoleId().equals(Constant.USER)) {

            return meetingDao.appFindList(pages, userId, meetingEntity);

        }
        //代表团
        if (userRoleEntity.getRoleId().equals(Constant.DELEGATION)) {

            SysUserEntity userEntity = userDao.selectById(userId);
            System.out.println(userEntity.getUsername());
            //获取所有代表团的userId
            List<String> userIds = userInfoDao.selectListByDelegation(userEntity.getUsername());
            System.out.println(userIds);
            return meetingDao.appFindListByDelegation(pages, userIds, meetingEntity);
        }
        //管理员
        if (userRoleEntity.getRoleId().equals(Constant.ADMIN)) {

            return meetingDao.adminFindList(pages, meetingEntity);
        }
        throw new RRException("该角色不允许访问");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void meetingSave(MeetingEntity meeting) {
        //先保存基本信息
        if (meeting.getMeetingId() == null) {
            meetingDao.insert(meeting);
            System.out.println("会议ID:" + meeting.getMeetingId());

            //会议添加完成之后 添加默认菜单
            MeetingMenuEntity menuEntity = new MeetingMenuEntity();
            menuEntity.setMeetingId(meeting.getMeetingId());
            menuEntity.setMenuStatus(true);
            menuEntity.setMenuName("议题");
            menuEntity.setMenuIcon("icon-biaojue");
            menuEntity.setMenuSort(2);
            meetingMenuDao.insert(menuEntity);

            menuEntity.setMenuId(null);
            menuEntity.setMenuName("文件");
            menuEntity.setMenuIcon("icon-zhusuxinxi");
            menuEntity.setMenuSort(4);
            meetingMenuDao.insert(menuEntity);

            menuEntity.setMenuId(null);
            menuEntity.setMenuName("请假");
            menuEntity.setMenuIcon("icon-yaoqing");
            menuEntity.setMenuSort(11);
            meetingMenuDao.insert(menuEntity);

            menuEntity.setMenuStatus(false);
            menuEntity.setMenuId(null);
            menuEntity.setMenuName("日程");
            menuEntity.setMenuIcon("icon-huiyiricheng1");
            menuEntity.setMenuSort(3);
            meetingMenuDao.insert(menuEntity);

            menuEntity.setMenuId(null);
            menuEntity.setMenuName("视频");
            menuEntity.setMenuIcon("icon-shipin-");
            menuEntity.setMenuSort(5);
            meetingMenuDao.insert(menuEntity);

            menuEntity.setMenuId(null);
            menuEntity.setMenuName("简报");
            menuEntity.setMenuIcon("icon-huiyiricheng");
            menuEntity.setMenuSort(6);
            meetingMenuDao.insert(menuEntity);

            menuEntity.setMenuId(null);
            menuEntity.setMenuName("签到记录");
            menuEntity.setMenuIcon("icon-huiyiricheng");
            menuEntity.setMenuSort(7);
            meetingMenuDao.insert(menuEntity);

            menuEntity.setMenuId(null);
            menuEntity.setMenuName("服务");
            menuEntity.setMenuIcon("icon-lianxiren");
            menuEntity.setMenuSort(8);
            meetingMenuDao.insert(menuEntity);

            menuEntity.setMenuId(null);
            menuEntity.setMenuName("住宿");
            menuEntity.setMenuIcon("icon-zhusu");
            menuEntity.setMenuSort(9);
            meetingMenuDao.insert(menuEntity);

            menuEntity.setMenuId(null);
            menuEntity.setMenuName("就餐");
            menuEntity.setMenuIcon("icon-ziyuan1");
            menuEntity.setMenuSort(10);
            meetingMenuDao.insert(menuEntity);


        } else {
            meetingDao.updateById(meeting);
            String attendeeIds = meeting.getAttendeeId();
            String attendeeGroupIds = meeting.getAttendeeGroupId();
            if (attendeeIds != null) {
                JSONArray jsonArrayAttendeeIds = JSONArray.fromObject(attendeeIds);
                JSONArray jsonArrayAttendeeGroupIds = JSONArray.fromObject(attendeeGroupIds);
                //直接保存参会人员
                if (jsonArrayAttendeeIds.size() > 0) {
                    //删除之前会议人员   重新存储
                    meetingUserDao.delectByMeetingId(meeting.getMeetingId(), 0);
                    //遍历获取所有会议人员
                    for (int i = 0; i < jsonArrayAttendeeIds.size(); i++) {
                        //保存会议人员
                        MeetingUserEntity meetingUserEntity = new MeetingUserEntity();
                        meetingUserEntity.setMeetingId(meeting.getMeetingId());
                        meetingUserEntity.setMeetingUserId(jsonArrayAttendeeIds.getJSONObject(i).getString("attendeeId"));
                        meetingUserEntity.setMeetingUserName(jsonArrayAttendeeIds.getJSONObject(i).getString("attendeeName"));
                        meetingUserDao.insert(meetingUserEntity);
                    }
                } else {
                    // 无会议人员的情况  删除存储的会议人员
                    meetingUserDao.delectByMeetingId(meeting.getMeetingId(), 0);
                }
                //群组情况下保存参会人员
                if (jsonArrayAttendeeGroupIds.size() > 0) {
                    meetingUserDao.delectByMeetingId(meeting.getMeetingId(), 1);
                    for (int i = 0; i < jsonArrayAttendeeGroupIds.size(); i++) {
                        UserGroupEntity userGroupEntity = userGroupDao.selectById(jsonArrayAttendeeGroupIds.getJSONObject(i).getString("attendeeGroupId"));
                        JSONArray jsonArray = JSONArray.fromObject(userGroupEntity.getGroupUserId());
                        for (int j = 0; j < jsonArray.size(); j++) {
                            //保存会议人员
                            MeetingUserEntity meetingUserEntity = new MeetingUserEntity();
                            meetingUserEntity.setMeetingId(meeting.getMeetingId());
                            meetingUserEntity.setMeetingUserId(jsonArray.getJSONObject(i).getString("groupUserId"));
                            meetingUserEntity.setMeetingUserName(jsonArray.getJSONObject(i).getString("groupUser"));
                            meetingUserEntity.setStatus(1);
                            meetingUserDao.insert(meetingUserEntity);
                        }
                    }
                } else {
                    meetingUserDao.delectByMeetingId(meeting.getMeetingId(), 1);
                }
            }
        }


    }
}